Code:
//bluatigro 29 dec 2016
//Canvas.h
//win32 graphics class
#include <windows.h> #ifndef CANVAS_H
#define CANVAS_H
class Canvas
{
public :
HDC hdc ;
Canvas(){}
~Canvas(){}
void ellipse(
int mx , int my , int dx , int dy ,
int clrin , int clrout , int thick , bool center = false ) ;
void line(
int x1 , int y1 , int x2 , int y2 ,
int clr , int thick ) ;
void triangle(
int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ,
int clrin , int clrout , int thick ) ;
void rectangle(
int left , int top , int right , int bottom ,
int clrin , int clrout , int thick , bool center = false ) ;
} ;
Canvas canvas ;
// primary colors
const int BLACK = 0x000000 ;
const int RED = 0x0000ff ;
const int GREEN = 0x00ff00 ;
const int YELLOW = 0x00ffff ;
const int BLUE = 0xff0000 ;
const int MAGENTA = 0xff00ff ;
const int CYAN = 0xffff00 ;
const int WHITE = 0xffffff ;
// mixed colors
const int PINK = 0x7f7fff ;
const int ORANGE = 0x007fff ;
const int PURPLE = 0x7f007f ;
const int GRAY = 0x7f7f7f ;
int colorMix( int clr1 , float f , int clr2 ) ;
#endif
Canvas.cpp
Code:
/
//bluatigro 29 dec 2016
//Canvas.cpp
//win32 graphics class#include "Canvas.h"
void Canvas::ellipse(
int mx , int my , int dx , int dy ,
int clrin , int clrout , int thick , bool center )
{
HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
HBRUSH newbrush = CreateSolidBrush( clrin ) ;
HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
if ( center )
{
Ellipse( hdc , mx - dx / 2 , my - dy / 2
, mx + dx / 2 , my + dy / 2 ) ;
}
else
Ellipse( hdc , mx , my , dx , dy ) ;
SelectObject( hdc, oldpen ) ;
SelectObject( hdc, oldbrush ) ;
DeleteObject( newpen ) ;
DeleteObject( newbrush ) ;
}
void Canvas::line(
int x1 , int y1 , int x2 , int y2 ,
int clr , int thick )
{
HPEN newpen = CreatePen( PS_SOLID , thick , clr ) ;
HPEN oldpen = (HPEN)SelectObject( hdc , newpen ) ;
MoveTo( hdc , x1 , y1 , NULL ) ;
LineTo( hdc , x2 , y2 ) ;
SelectObject( hdc , oldpen ) ;
DeleteObject( newpen ) ;
}
void Canvas::triangle(
int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ,
int clrin , int clrout , int thick )
{
HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
HBRUSH newbrush = CreateSolidBrush( clrin ) ;
HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
POINT p[] = { { x1 , y1 } ,
{ x2 , y2 } ,
{ x3 , y3 } } ;
Polygon( hdc , p , 3 ) ;
SelectObject( hdc, oldpen ) ;
SelectObject( hdc, oldbrush ) ;
DeleteObject( newpen ) ;
DeleteObject( newbrush ) ;
}
void rectangle(
int x1 , int y1 , int x2 , int y2 ,
int clrin , int clrout , int thick , bool center )
{
HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
HBRUSH newbrush = CreateSolidBrush( clrin ) ;
HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
if ( center )
Rectangle( hdc , x1 - x2 / 2 , y1 - y2 / 2 , x1 + x2 / 2 , y1 + y2 / 2 ) ;
else
Rectangle( hdc , x1 , y1 , x2 , y2 ) ;
SelectObject( hdc, oldpen ) ;
SelectObject( hdc, oldbrush ) ;
DeleteObject( newpen ) ;
DeleteObject( newbrush ) ;
}
int colorMix( int clr1 , float f , int clr2 )
{
int r1 , g1 , b1 , r2 , g2 , b2 ;
r1 = clr1 & 0xff ;
g1 = ( clr1 >> 8 ) & 0xff ;
b1 = ( clr1 >> 16 ) & 0xff ;
r2 = clr2 & 0xff ;
g2 = ( clr2 >> 8 ) & 0xff ;
b2 = ( clr2 >> 16 ) & 0xff ;
r1 = r1 + ( r2 - r1 ) * f ;
g1 = g1 + ( g2 - g1 ) * f ;
b1 = b1 + ( b2 - b1 ) * f ;
return r1 + g1 * 0x100 + b1 * 0x10000 ;
}